﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using CommonLib;

namespace PadiCalClient
{
    [Serializable]
    public class Reservation
    {
        private int id;

        public bool clean_flag = false;

        private string name;

        private string state;

        private User host;

        IDictionary<string, User> _users = new Dictionary<string, User>();

        public bool abortAll = false;

        public string toAbort;

        private ArrayList tosend = new ArrayList();

        private ArrayList users;

        private ArrayList initialSlots;

        string currentSl = null;

        public Reservation(string Rname, ArrayList Rusers, ArrayList Rslots, User Rhost, int i)
        {
            id = i;
            this.name = Rname;
            this.state = "Initiated";
            this.users = Rusers;
            this.initialSlots = Rslots;
            this.host = Rhost;

            this.addKnownUser(Rhost);
        }

        public int getID()
        {
            return id;
        }

        public string getHostName()
        {
            return Host.Name;
        }

        public void addKnownUser(User user)
        {
            if (this._users.ContainsKey(user.Name))
                this._users[user.Name] = user;
            else
                this._users.Add(user.Name, user);

        }

        public IDictionary<string, User> getUsers()
        {
            return this._users;
        }

        public User getUser(string u)
        {
            return this._users[u];
        }

        public bool hasKnownUser(string u)
        {
            return this._users.ContainsKey(u);
        }

        public ArrayList invUsers
        {
            get { return this.users; }
            set { this.users = value; }

        }

        public ArrayList Propagation
        {
            get { return this.tosend; }
            set { this.tosend = value; }
        }

        public string Name{
            get { return name;}
            set { name = value; }

        }

        public string State
        {
            get { return state; }
            set{ state = value;}
        }

        public ArrayList Slots
        {
            get { return this.initialSlots; }
            set { this.initialSlots = value; }
        }

        public User Host
        {
            get { return this.host; }
            //set { this.host = value; }
        }

        public string currentSlot
        {
            get { return this.currentSl; }
            set { this.currentSl = value; }
        }

        /* Init para TEntatively */
        public void changeToTentatively(ArrayList Rslots)
        {
            clean_flag = false;
            this.state = "Tentatively";
            this.Slots = Rslots;
            this.currentSlot = (string) this.initialSlots[0];
            this.initialSlots.Remove(this.currentSlot);
        }

        public void changeToCommit()
        {
            clean_flag = false;
            this.state = "Commit";
        }

        public void changeToCommited()
        {
            clean_flag = false;
            this.state = "Commited";
        }

        public bool changeSlot()
        {
            clean_flag = false;
            if (this.currentSl != null)
            {
                if (this.initialSlots.Count != 0)
                {
                    string change = (string) this.initialSlots[0];
                    this.initialSlots.RemoveAt(0);
                    this.currentSl = change;
                    state = "Tentatively";
                    return true;
                }
            }
            return false;
        }

        public object Clone()
        {
            return CloneHelpers.DeepClone(this);
        }
    }
}
